package mamikeji.liaowa.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.ToDoBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.adapter.ToDoBaseExpandableListAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MultiStateView;

public class ToDoActivity extends BaseActivity {
    private static final String TAG = "ToDoActivity";

    private String category_type;

    private ExpandableListView expandableListView_todo;
    private MultiStateView multiStateView_todo;
    private ToDoBaseExpandableListAdapter toDoBaseExpandableListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.todo_name);

        Intent mIntent = getIntent();
        category_type = mIntent.getStringExtra("category");


        multiStateView_todo = (MultiStateView) findViewById(R.id.multiStateView_todo);
        expandableListView_todo = (ExpandableListView) findViewById(R.id.expandableListView_todo);
        expandableListView_todo.setGroupIndicator(null);
        expandableListView_todo.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
        getTodoDate();

        multiStateView_todo.getView(MultiStateView.VIEW_STATE_ERROR).findViewById(R.id.button_error_refish)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        multiStateView_todo.setViewState(MultiStateView.VIEW_STATE_LOADING);
                        getTodoDate();
                    }
                });
    }

    /**
     * 获取待办任务数据
     */
    private void getTodoDate() {
        ForLog.i("HH:mm:ssHH:mm:ss",""+new SimpleDateFormat("HH:mm:ss").format(new Date()));
        //参数集合
        Map<String, String> formMap = new HashMap<String, String>();
        formMap.put(HttpKey.TODO_USERCODE, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.TODO_CATEGORY, category_type);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.TODO_ALL, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
//                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                multiStateView_todo.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                ForLog.i("HH:mm:ssHH:mm:ss",""+new SimpleDateFormat("HH:mm:ss").format(new Date()));
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ToDoBean toDoBean = new Gson().fromJson(response, ToDoBean.class);
                        if (toDoBean.getList() != null && toDoBean.getList().size() > 0) {
                            List<ToDoBean.ListEntity> list = toDoBean.getList();
                            toDoBaseExpandableListAdapter = new ToDoBaseExpandableListAdapter(ToDoActivity.this, list);
                            expandableListView_todo.setAdapter(toDoBaseExpandableListAdapter);
//                            toDoBaseExpandableListAdapter.setOnItemClickListener(new ToDoBaseExpandableListAdapter.OnItemClickListener() {
//                                @Override
//                                public void onSingleItemClick(TodoChildSelectAdapter adapter, View view, int groupPosition, int position, int type) {
//                                    int selected = adapter.getItem(position).getSelected();
//                                    if (selected == 0) {
//                                        adapter.getItem(position).setSelected(1);
//                                        for (int i = 0; i < adapter.getList().size(); i++) {
//                                            if (i != position) {
//                                                adapter.getItem(i).setSelected(0);
//                                            }
//                                        }
//                                    } else {
//                                        adapter.getItem(position).setSelected(0);
//                                    }
//                                    adapter.notifyDataSetChanged();
//                                    updateToDoData(groupPosition, position, type);
//                                }
//
//                                @Override
//                                public void onMultipleItemClick(TodoChildSelectAdapter adapter, View view, int groupPosition, int position, int type) {
//                                    int selected = adapter.getItem(position).getSelected();
//                                    if (selected == 0) {
//                                        int count = 0;
//                                        for (int i = 0; i < adapter.getList().size(); i++) {
//                                            if (adapter.getItem(i).getSelected() == 1) {
//                                                count++;
//                                            }
//                                        }
//                                        if (count >= 5) {
//                                            ForToast.showToast("最多只能选择5个");
//                                            return;
//                                        }
//                                        adapter.getItem(position).setSelected(1);
//                                    } else {
//                                        adapter.getItem(position).setSelected(0);
//                                    }
//                                    adapter.notifyDataSetChanged();
//                                    updateToDoData(groupPosition, position, type);
//                                }
//
//                                @Override
//                                public void onToDoYesClick(View view, int groupPosition, boolean isExpanded) {
//                                    if (isExpanded) {
//                                        expandableListView_todo.collapseGroup(groupPosition);
//                                    } else {
//                                        expandableListView_todo.expandGroup(groupPosition);
//                                    }
//                                }
//
//                                @Override
//                                public void onToDoNoClick(View view, int groupPosition, ToDoBean.ListEntity groupBean, boolean isExpanded) {
//                                    if (isExpanded) {
//                                        int type = groupBean.getTType();
//                                        if (type != 1) {
//                                            List<ToDoBean.ListEntity.Task_optionsListEntity> childList = groupBean.getTask_optionsList();
//                                            if (childList != null && childList.size() > 0) {
//                                                for (int i = 0; i < childList.size(); i++) {
//                                                    childList.get(i).setSelected(0);
//                                                }
//                                                toDoBaseExpandableListAdapter.notifyDataSetChanged();
//                                            }
//                                        }
//                                    }
//                                }
//                            });
//                            toDoBaseExpandableListAdapter.setOnSeekBarChangeListener(new ToDoBaseExpandableListAdapter.OnSeekBarChangeListener() {
//                                @Override
//                                public void onSeekBarChangeListener(SeekBar seekBar, int i, int groupPosition, int childPosition) {
//                                    updateToDoData(groupPosition, i, 1);
//                                }
//                            });

                            multiStateView_todo.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            System.out.println("为空");
                            multiStateView_todo.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    } else {
                        multiStateView_todo.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void updateToDoData(int groupPosition, int position, int type) {
        showLoading();
        List<ToDoBean.ListEntity> groupList = toDoBaseExpandableListAdapter.getList();
        ToDoBean.ListEntity groupBean = groupList.get(groupPosition);
        ToDoBean.ListEntity.Task_optionsListEntity childBean = groupBean.getTask_optionsList().get(position);
        Map<String, String> formMap = new HashMap<>();
        formMap.put("userid",  ForPreference.getString(ForContents.USER_CODE));
        formMap.put("tid", groupBean.getTid());
        formMap.put("timeid", "0");
        formMap.put("oid", childBean.getId());
        formMap.put("scoreLv", childBean.getScoreLv() + "");
        formMap.put("score", groupBean.getScore() + "");
        formMap.put("ttype", type + "");
        formMap.put("config_id", groupBean.getConfig_id());
        formMap.put("category", category_type);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.TODO_ADD, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                hideLoading();
                ForToast.showToast(R.string.intent_error);
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                hideLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
